package com.enjoy.async;

import com.alibaba.dubbo.config.annotation.Service;
import com.xiangxue.jack.async.AsyncService;

import java.util.concurrent.CompletableFuture;

/**
 * <p>TODO</p>
 *
 * @Author Ellison Pei
 * @Date 2021/7/22 17:10
 **/
@Service
public class AsyncServiceImpl implements AsyncService {
    @Override
    public String asynctoDo(String name) {

        for (int i = 0; i < 1000; i++) {
            System.out.println("===================AsyncServiceImpl.asynctoDo");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "hello," + name;
    }

    @Override
    public CompletableFuture<String> doOne(String s) {
        long startTime = System.currentTimeMillis();
        sleep(5);
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.completedFuture("doOne()===========");
        long endTime = System.currentTimeMillis();
        long userTimeMill = endTime - startTime;
        System.out.println("doOne()方法执行用时：" + userTimeMill);
        return stringCompletableFuture;
    }

    @Override
    public String doTwo(String s) {
        sleep(9);
        System.out.println("======doTwo.params========" + s);
        return "doTwo===OK=========" + s;
    }


    private void sleep(int seconds) {
        try {
            Thread.sleep(seconds * 1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
